//
//  HGBaiduGeolocation.m
//  HGMM
//
//  Created by huan liu on 2019/4/19.
//  Copyright © 2019 xwx. All rights reserved.
//

#import "HGBaiduGeolocation.h"
#import <BMKLocationkit/BMKLocationComponent.h>
#import "NSDictionary+json.h"

@interface HGBaiduGeolocation ()<BMKLocationAuthDelegate,BMKLocationManagerDelegate>

@property(nonatomic,strong)BMKLocationManager *locationManager;

@end


@implementation HGBaiduGeolocation

-(void)getCurrentPosition: (CDVInvokedUrlCommand *)command{
    
    //do nothings
    
    
    //邢伟新个人key: 0Ts4XwgcqeitvTm1B9VFvVSavjXWszds
    //公用key: LQE7aon8DpyxdbtGHPCMYhg6FVCwjeQL
    
     [[BMKLocationAuth sharedInstance] checkPermisionWithKey:@"LQE7aon8DpyxdbtGHPCMYhg6FVCwjeQL" authDelegate:self];
    
    //初始化实例
    _locationManager = [[BMKLocationManager alloc] init];
    //设置delegate
    _locationManager.delegate = self;
    //设置返回位置的坐标系类型
    _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
    //设置距离过滤参数
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    //设置预期精度参数
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //设置应用位置类型
    _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    //设置是否自动停止位置更新
    _locationManager.pausesLocationUpdatesAutomatically = NO;
    //设置是否允许后台定位
    //_locationManager.allowsBackgroundLocationUpdates = YES;
    //设置位置获取超时时间
    _locationManager.locationTimeout = 10;
    //设置获取地址信息超时时间
    _locationManager.reGeocodeTimeout = 10;
    
    
    [self.locationManager requestLocationWithReGeocode:(NO) withNetworkState:(NO) completionBlock:^(BMKLocation * _Nullable location, BMKLocationNetworkState state, NSError * _Nullable error) {
        
        
        if (!error) {
            
            CLLocation *cll = location.location;
            CLLocationCoordinate2D cll2D = cll.coordinate;
            
            
            CLLocationDegrees latitude = cll2D.latitude;
            CLLocationDegrees longitude = cll2D.longitude;
            NSString *latitudeString = [[NSNumber numberWithDouble:latitude] stringValue];
            NSString *longitudeString = [[NSNumber numberWithDouble:longitude] stringValue];
            
            NSDictionary *dic = @{
                                  @"latitude":latitudeString,
                                  @"longitude":longitudeString
                                  };
            NSString *result = [dic toReadableJSONString];
            
            CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:result];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            
    
        }else{
            
            CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"获取未知信息失败"];
            [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
            
        }
        
    }];
    
}

@end
